package com.wh.wisdomsite.xbox.utils;

import com.wh.wisdomsite.xbox.utils.page.PageView;

import java.util.HashMap;
import java.util.Map;

/**
 * Redis 中 key 定义类
 *
 * @version 1.0 add by jsk at 2015-01-08
 **/
public class RedisKeyUtils {
    public static String ARTICLE_ISFIRST_LOADS = "article:isfirst:loads"; // 文章是否第一次加载;
    private static String ARTICLE_FIELD = "article:field"; // 文章管理redis field
    private static String ARTICLE_KEY = "article:key:"; // 文章管理redis key
    private static String ARTICLE_SET_KEY = "article:set:key"; // 文章管理set key

    public static String ARTICLECLASS_ISFIRST_LOADS = "articleclass:isfirst:loads"; // 网站栏目是否第一次加载;
    private static String ARTICLECLASS_FIELD = "articleclass:field"; // 网站栏目redis
    // field
    private static String ARTICLECLASS_KEY = "articleclass:key:"; // 网站栏目redis
    // key
    private static String ARTICLECLASS_SET_KEY = "articleclass:set:key"; // 网站栏目set
    // key

    public static String ANNOUNCE_ISFIRST_LOADS = "announce:isfirst:loads"; // 公告是否第一次加载;
    private static String ANNOUNCE_FIELD = "announce:field"; // 公告redis field
    private static String ANNOUNCE_KEY = "announce:key:"; // 公告redis key
    private static String ANNOUNCE_SET_KEY = "announce:set:key"; // 公告set key

    public static String NEWS_ISFIRST_LOADS = "news:isfirst:loads"; // 新闻是否第一次加载;
    private static String NEWS_FIELD = "news:field"; // 新闻redis field
    private static String NEWS_KEY = "news:key:"; // 新闻redis key
    private static String NEWS_SET_KEY = "news:set:key"; // 新闻set key

    /*
     * public static String ACCOUNTLOG_ISFIRST_LOADS =
     * "accountlog:isfirst:loads"; //资金记录是否第一次加载; private static String
     * ACCOUNTLOG_FIELD = "accountlog:field"; // 资金记录redis field private static
     * String ACCOUNTLOG_KEY = "accountlog:key:"; // 资金记录redis key private
     * static String ACCOUNTLOG_SET_KEY = "accountlog:set:key"; // 资金记录set key
     */
	/*
	 * public static String ACCOUNTCASH_ISFIRST_LOADS =
	 * "accountcash:isfirst:loads"; //提现记录是否第一次加载; private static String
	 * ACCOUNTCASH_FIELD = "accountcash:field"; // 提现记录redis field private
	 * static String ACCOUNTCASH_KEY = "accountcash:key:"; // 提现记录redis key
	 * private static String ACCOUNTCASH_SET_KEY = "accountcash:set:key"; //
	 * 提现记录set key
	 */
	/*
	 * public static String RECHARGERECORD_ISFIRST_LOADS =
	 * "rechargeRecord:isfirst:loads"; //充值记录是否第一次加载; private static String
	 * RECHARGERECORD_FIELD = "rechargeRecord:field"; // 充值记录redis field private
	 * static String RECHARGERECORD_KEY = "rechargeRecord:key:"; // 充值记录redis
	 * key private static String RECHARGERECORD_SET_KEY =
	 * "rechargeRecord:set:key"; // 充值记录set key
	 */
    private static String SOCKET_BORROW_FILD = "socket:borrow:fild";
    private static String SOCKET_BORROW_KEY = "socket:borrow:key";

    public static final int DEFAULT_LIFE_TIMES = 60 * 60 * 60 * 1000; // 默认生命周期是1天

    // 文章管理redis articleId key
    public static String getArticleKey(int articleId) {
        StringBuilder sb = new StringBuilder();
        sb.append(ARTICLE_KEY).append(articleId);
        return sb.toString();
    }

    // 文章管理redis field
    public static String getArticleField() {
        return ARTICLE_FIELD;
    }

    // 网站栏目redis field
    public static String getArticleClassField() {
        return ARTICLECLASS_FIELD;
    }

    // 网站栏目redis field
    public static String getArticleClassKey(int classId) {
        StringBuilder sb = new StringBuilder();
        sb.append(ARTICLECLASS_KEY).append(classId);
        return sb.toString();
    }

    public static String getAnnounceField() {
        return ANNOUNCE_FIELD;
    }

    // 网站栏目redis field
    public static String getAnnounceKey(int id) {
        StringBuilder sb = new StringBuilder();
        sb.append(ANNOUNCE_KEY).append(id);
        return sb.toString();
    }

    public static String getNewsField() {
        return NEWS_FIELD;
    }

    public static String getNewsKey(long id) {
        StringBuilder sb = new StringBuilder();
        sb.append(NEWS_KEY).append(id);
        return sb.toString();
    }

    /**
     * 数据库中的分页与redis 中分布转换
     *
     * @param pageview
     * @return
     */
    public static Map<String, String> getJedisPage(PageView pageview) {
        Map<String, String> pageMap = new HashMap<String, String>();
        int pageSize = 20;
        int pageNow = 1;
        int startRow = 0;
        int endRow = 0;
        if (pageNow <= 1) {
            startRow = 0;
            endRow = pageSize - 1;
        } else {
            startRow = (pageSize * pageNow) - (pageSize);
            endRow = (pageSize * pageNow) - 1;
        }
        pageMap.put("startRow", String.valueOf(startRow));
        pageMap.put("endRow", String.valueOf(endRow));
        System.out.println("startRow = " + startRow + "endRow = " + endRow);
        return pageMap;
    }

    // 文章管理set key
    public static String getArticleSetKey() {
        return ARTICLE_SET_KEY;
    }

    // 公告set key
    public static String getAnnounceSetKey() {
        return ANNOUNCE_SET_KEY;
    }

    // 网站栏目set key
    public static String getArticleclassSetKey() {
        return ARTICLECLASS_SET_KEY;
    }

    // 新闻set key
    public static String getNewsSetKey() {
        return NEWS_SET_KEY;
    }

    // socket hash filed
    public static String getSocketBorrowFiled() {
        return SOCKET_BORROW_FILD;
    }

    public static String getSocketKey(String sessionId) {
        StringBuilder sb = new StringBuilder();
        sb.append(SOCKET_BORROW_KEY).append(":").append(sessionId);
        return sb.toString();
    }

}
